#!/usr/bin/python
# Filename: 19.py
import urllib, Image, StringIO, difflib

# data = urllib.urlopen('http://huge:file@www.pythonchallenge.com/pc/return/balloons.jpg')
# im = Image.open(StringIO.StringIO(data.read()))

# w, h = 375, 335
# im1 = Image.new(im.mode, (w, h))
# im2 = Image.new(im.mode, (w, h))

# for i in range(w):
 	# for j in range(h):
 		# im1.putpixel((i, j), im.getpixel((i, j)))
# im1.save('D:\\18.1.jpg')

# for i in range(w):
 	# for j in range(h):
 		# im2.putpixel((i, j), im.getpixel((w+i, j)))
 		
# im2.save('D:\\18.2.jpg')

# fp1 = open('D:\\18.1.jpg')
# fp2 = open

#step 1
fp = open('D:\\delta.txt')
im1 = open('D:\\18.out.1.png', 'wb')
im2 = open('D:\\18.out.2.png', 'wb')
im3 = open('D:\\18.out.delta.png', 'wb')

textlines = [line.rstrip('\n') for line in fp]
print textlines[0:3]
text_lines_1 = [line.split('   ', 1)[0] for line in textlines]
text_lines_2 = [line.split('   ', 1)[1] for line in textlines]
print len(text_lines_1)
print len(text_lines_2)
print text_lines_1[0:10]
d = difflib.Differ()
res = list(d.compare(text_lines_1, text_lines_2))
for line in res:
	if(line.find('-') != -1):
		for item in line.strip('- ').split():
			im1.write(chr(int(item, 16)))
	elif(line.find('+') != -1):
		for item in line.strip('+ ').split():
			im2.write(chr(int(item, 16)))
	else:
		for item in line.strip(' ').split():
			im3.write(chr(int(item, 16)))
im1.close(), im2.close(), im3.close()
#butter, fly, /hex/bin.html
# for line in fp:
	# l1, l2 =line.split('   ')
	# if(l1 == l2):
		# for item in l1.split():
			# im.write(chr(int(item, 16)))
	# else:
		# for px1, px2 in zip(l1.split(), l2.split()):
			# im.write(chr(abs(int(px1, 16)-int(px2, 16))))
# im.close()
# fp.close()

#step 2
		
# im1 = Image.open('D:\\18.1.png')
# im2 = Image.open('D:\\18.2.png')
# w, h = im1.size
# im = Image.new(im1.mode, im1.size)
# for x in range(w):
	# for y in range(h):
		# print x, y
		# p1 = im1.getpixel((x, y))
		# p2 = im2.getpixel((x, y))
		# p = tuple([px1+px2 for px1, px2 in zip(p1, p2)])
		# im.putpixel((x, y), p)

		# print 'hello'
		# print x, y
# im.save('D:\\18.out.png')